package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;

import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {


    @Reference
    private UserService userService;


    @RequestMapping("/getUsername")
    public Result getUsername(){
        try {
            org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,user.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_USERNAME_FAIL);
        }
    }



    @RequestMapping("/add")
    public Result add(@RequestBody User user, Integer[] RoleIds){
        try {
            userService.add(user,RoleIds);
        } catch (Exception e) {
            // e.printStackTrace();
            return new Result(false,MessageConstant.ADD_USER_FAIL);
        }
        return new Result(true,MessageConstant.ADD_USER_SUCCESS);
    }

    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = userService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());

        return pageResult;
    }


    //根据id查询
    @RequestMapping("/findById")
    public Result findById(Integer id){
        User user = userService.findById(id);
        if(user != null){
            Result result = new Result(true, MessageConstant.QUERY_USER_SUCCESS);
            result.setData(user);
            return result;
        }
        return new Result(false,MessageConstant.QUERY_USER_FAIL);
    }

    //根据检查组id查询检查项id
    @RequestMapping("/findroleIdsByUserId")
    public Result findroleIdsByUserId(Integer id){
        try {
            List<Integer> UserIds = userService.findRoleByUserId(id);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,UserIds);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user, Integer[] roleIds){

        try {
            userService.edit(user,roleIds);
        } catch (Exception e) {
            //e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_USER_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_USER_SUCCESS);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            userService.delete(id);
        }catch (RuntimeException e){
            return new Result(false,e.getMessage());
        } catch (Exception e) {
            return new Result(false,MessageConstant.DELETE_USER_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_USER_SUCCESS);
    }


    @RequestMapping("/findAll")
    public Result findAll(){

        List<User> userList =  userService.findAll();
        if(userList != null && userList.size()>0){
            Result result = new Result(true,MessageConstant.QUERY_USER_SUCCESS);
            result.setData(userList);
            return result;
        }
        return new Result(false,MessageConstant.QUERY_USER_FAIL);
    }


}
